classValidatorToJsonSchema - Get JSON Schema for a specific class#48
classValidatorToJsonSchema - Get JSON Schema for a specific class#48jan-demsar wants to merge 1 commit intoepiphone:masterfrom
Conversation
There was a problem hiding this comment.
Thanks for the contribution! This will totally be a useful addition. Left a few comments.
Also, does this work with nested validation? https://github.com/typestack/class-validator#validating-nested-objects. I have a feeling it might not because of the target metadata filtering in https://github.com/epiphone/class-validator-jsonschema/pull/48/files#diff-a2a171449d862fe29692ce031981047d7ab755ae7f84c707aef80701b3ea0c80R79.
| moduleFileExtensions: ['ts', 'js', 'json'], | ||
| roots: ['<rootDir>/__tests__'], | ||
| testPathIgnorePatterns: ['/node_modules/'], | ||
| testPathIgnorePatterns: ['/node_modules/', '/__tests__/classes'], |
| export class User { | ||
| @MinLength(5) | ||
| @IsString() | ||
| name: string; | ||
| } |
There was a problem hiding this comment.
Is it possible to use namespaces in this file to import two classes with the same name? Might be clearer than declaring one here and another in a separate file.
I mean something like
namespace Foo { export class User { } }
namespace Bar { export class User { } }| const targetMetadatas = _(metadatas) | ||
| .filter(metadata => { | ||
| return metadata.target === target | ||
| }).value(); |
There was a problem hiding this comment.
Let's use the native Array.filter here instead of lodash
| /** | ||
| * Convert class-validator class into JSON Schema definition. | ||
| */ | ||
| export type ClassType<T> = new (...args: any[]) => T; |
There was a problem hiding this comment.
| export type ClassType<T> = new (...args: any[]) => T; | |
| export type ClassType = new (...args: any[]) => any; |
We can omit the unused generic here and below I think.
| export class User { | ||
| @MinLength(5) | ||
| @IsString() | ||
| name: string; | ||
| } |
There was a problem hiding this comment.
Can we also test that if the two User classes both have a property with the same name but different class-validator decorators, only the desired decorators get applied in the resulting schema?
Hello.
First of all, thank you for amazing package! It is very useful, but in my case I needed ability to extract JSON Schema for a specific class (with duplicate names).
The problem with
validationMetadatasToSchemasis that it merges schemas with the same names.So if I have 1 User class with
nameproperty and another withfirstName, JSON Schema will include both (obviously I could use unique names, but for current project I cannot avoid duplicates).As a workaround I created a separate function which solves the issue for me and might be useful for someone else as well.